#include <QCoreApplication>
#include <QObject>
#include <QDebug>
#include <QThread>

#include <QLibSsh/sshshellconnection.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // 单线程调用

    {
        auto sshConncetion = new SshShellConnection(
            {
                .user = "shz",
                .host = "172.22.78.205",
                .port = 22,
                .password = "shz0905",
            }, &a);

        QObject::connect(sshConncetion, &SshShellConnection::SshConnected, [&sshConncetion]() {
            qDebug() << "sshConnected";
            sshConncetion->SendCommand("ls -l\n");
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshExecFinished, [&sshConncetion]() {
            qDebug() << "SshExecFinished";
            sshConncetion->DisconnectSsh();
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshExecStarted, [] {
            qDebug() << "SshExecStarted";
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshDisconnected, [&] {
            qDebug() << "SshDisconnected";
            sshConncetion->deleteLater();
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshStandardError, [](const std::string& error) {
            qDebug() << "SshStandardError, " << error;
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshStandardOutput, [](const std::string& output) {
            qDebug() << "SshStandardOutput, " << output;
        });

        sshConncetion->ConnectSsh();
    }


    // 多线程调用

    {
        auto sshConncetion = new SshShellConnection(
            {
                .user = "shz",
                .host = "172.22.78.205",
                .port = 22,
                .password = "shz0905",
        }, &a);

        const auto thread = new QThread(&a);

        sshConncetion->moveToThread(thread);
        QObject::connect(thread, &QThread::started, sshConncetion, &SshShellConnection::ConnectSsh);
        QObject::connect(sshConncetion, &SshShellConnection::SshConnected, [&] {
            qDebug() << "sshConnected";
            sshConncetion->SendCommand("ls -l\n");
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshExecFinished, [&sshConncetion]() {
            qDebug() << "SshExecFinished";
            sshConncetion->DisconnectSsh();
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshExecStarted, [] {
            qDebug() << "SshExecStarted";
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshDisconnected, [&] {
            qDebug() << "SshDisconnected";
            thread->quit();
            thread->wait();
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshStandardError, [](const std::string& error) {
            qDebug() << "SshStandardError, " << error;
        });

        QObject::connect(sshConncetion, &SshShellConnection::SshStandardOutput, [](const std::string& output) {
            qDebug() << "SshStandardOutput, " << output;
        });


        QObject::connect(thread, &QThread::finished, sshConncetion, &QObject::deleteLater);
        QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
        QObject::connect(thread, &QThread::finished, &a, &QCoreApplication::quit);
        thread->start();
    }


    return a.exec();
}
